<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户登录</title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">

    <script src="./js/config.js"></script>
    <script src="./js/jquery.min.js"></script>
</head>

<body>

    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div class="navbar-item">
                        <a class="button is-small is-primary" href="register.html">注册</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>


    <div class="container">
        <div class="box sign-box">
            <div>
                <div class="field">
                    <label class="label">用户名</label>
                    <div class="control">
                        <input class="input" id="username" type="text" autocomplete="off" placeholder="请输入用户名">
                    </div>
                </div>
                <div class="field">
                    <label class="label">密码</label>
                    <div class="control">
                        <!-- 禁止自动填充用户名密码 -->
                        <input type="password" style="position: absolute;left: 0;display: none" />
                        <input class="input" id="password" type="password" placeholder="请输入密码">
                    </div>
                </div>
                <div class="field is-grouped">
                    <div class="control">
                        <button class="button is-primary" onclick="OnLogin()">立即登录</button>
                    </div>
                    <div class="control">
                        <a href="register.html" class="button is-text">还没账号？</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

<script>
    function OnLogin() {
        // 1、先进行参数的非空校验
        var username = jQuery("#username");
        if (username.val().trim() == "") {
            // 此时用户名为空
            alert("当前用户名为空!");
            username.focus();
            return;
        }

        var password = jQuery("#password");
        if (password.val().trim() == "") {
            // 此时用户名为空
            alert("当前用户密码为空!");
            password.focus();
            return;
        }

        // 2、拿到登录的结果
        jQuery.ajax({
            url: base_url + "/user/login",
            method: "POST",
            data: {
                "username": username.val(),
                "password": password.val()
            },
            // 结果的返回 code、msg、data
            success: function (result) {
                if (result != null && result.code == 200) {
                    // 将前端返回的 token 进行存储
                    window.localStorage.setItem(token_key, result.data);
                    console.log(window.localStorage.getItem(token_key));
                    alert("用户登录成功！");
                    location.href = "./index.html";
                    return;
                }
                else {
                    alert("用户登录失败：" + result.msg);
                    return;
                }
            }
        });
    }
</script>


</html>